/**
 * hkx .
 *
 *
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mchange.v2.beans.BeansUtils;
import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.bmx.entity.ErrorEntity;
import io.renren.modules.bmx.entity.MoniRecordEntity;
import io.renren.modules.bmx.entity.SubjectEntity;
import io.renren.modules.bmx.entity.TemporaryEntity;
import io.renren.modules.bmx.service.*;
import io.renren.modules.bmx.vo.SubjectVo;
import io.renren.modules.bmx.vo.TemporarySubjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * APP测试接口
 *
 * @author HKX
 */
@RestController
@RequestMapping("/app")
@Api("APP测试接口")
public class AppMainController {

    @Autowired(required = false)
    private CartypeService cartypeService;
    @Autowired(required = false)
    private TemporaryService temporaryService;

    @Autowired(required = false)
    private SubjectService subjectService;

    @Autowired(required = false)
    private ErrorService errorService;

    @Autowired(required = false)
    private MoniRecordService moniRecordService;



    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser UserEntity user){
        return R.ok(user);
    }


    @Login
    @GetMapping("userLevels")
    @ApiOperation("获取用户所有的数据权限，包括车型、科目、题型、套题")
    public R userInfo1(@LoginUser UserEntity user){
        return R.ok(cartypeService.findUserAllLevels(user.getUserId()));
    }



    @Login
    @PostMapping("subjectVo/start-test")
    @ApiOperation("根据套题id，获取多个题目：用于顺序练习，随机练习等")
    public R getSubjectBySeIds(@RequestBody SecondtypeIdsRandomDto secondtypeIdsRandomDto, @LoginUser UserEntity user) {
        List<SubjectVo> subjectVoList = subjectService.getSubjectVoForTestExercise(secondtypeIdsRandomDto.getSecondtypeIds(), secondtypeIdsRandomDto.getRandom(), user.getUserId());
        return R.ok(subjectVoList);
    }


    @Login
    @PostMapping("temporary/update")
    @ApiOperation("根据 torder,修改temporary，用于答题。")
    public R update(@RequestBody TemporaryEntity temporary,@LoginUser UserEntity user){
        return R.ok(temporaryService.update(temporary, new UpdateWrapper<TemporaryEntity>().eq("uid", user.getUserId()).eq("torder", temporary.getTorder())));
    }

    @Login
    @GetMapping("temporary/count")
    @ApiOperation("判断是否有考题。")
    public R temporaryCount(@LoginUser UserEntity user){
        return R.ok(temporaryService.count(new QueryWrapper<TemporaryEntity>().eq("uid",user.getUserId())));
    }

    @Login
    @GetMapping("subjectVo/continue-test")
    @ApiOperation("继续做题")
    public R continueTest(@LoginUser UserEntity user) {
        List<SubjectVo> subjectVoList = subjectService.getSubjectVoForContinueTestExercise(user.getUserId());
        return R.ok(subjectVoList);
    }

    @Login
    @GetMapping("subjectVo/errors")
    @ApiOperation("获取当前登录用户的所有错题")
    public R listErrors(@LoginUser UserEntity user){
        List<SubjectVo> subjectVos = subjectService.getSubjectVoForErrorTestExercise((long)user.getUserId());
        return R.ok(subjectVos);
    }

    @Login
    @PostMapping("error/clear")
    @ApiOperation("清空该用户错题")
    public R clearError(@LoginUser UserEntity user){
        return R.ok(errorService.removeByMap(new MapUtils().put("uid", user.getUserId())));
    }

    @Login
    @GetMapping("subjectVo/current-errors")
    @ApiOperation("获取本次练习的错题")
    public R currentErrors(@LoginUser UserEntity user){
        List<SubjectVo> subjectVos = subjectService.getSubjectVoForCurrentError((long)user.getUserId());
        return R.ok(subjectVos);
    }


    @Login
    @PostMapping("error/save")
    @ApiOperation("添加错题，只需要传题目id")
    public R saveError(@RequestBody SubjectEntity subjectEntity, @LoginUser UserEntity user){
        ErrorEntity entity = errorService.getOne(new QueryWrapper<ErrorEntity>().eq("ssid", subjectEntity.getId()));
        if(entity==null){
            errorService.save(new ErrorEntity().setUid(user.getUserId().intValue()).setSsid(subjectEntity.getId()));
        }
        return R.ok();
    }


    @Login
    @GetMapping("error/count")
    @ApiOperation("获取用户错题的数量")
    public R countError(@LoginUser UserEntity user){
        return R.ok(errorService.count(new QueryWrapper<ErrorEntity>().eq("uid", user.getUserId())));
    }

    @Login
    @PostMapping("test/end")
    @ApiOperation("结束练习，提交练习")
    public R exitTest(@LoginUser UserEntity user){
        return R.ok(temporaryService.removeByMap(new MapUtils().put("uid",user.getUserId())));
    }




    @Login
    @PostMapping("subjectVo/moni-test")
    @ApiOperation("模拟考试，只需要传科目的id主键，格式{\"id\":1}")
    public R getSubject4MoniTest(@RequestBody PramsDto pramsDto, @LoginUser UserEntity user) {
        List<SubjectVo> subjectVoList = subjectService.getSubjectVoForMoiTest(pramsDto.getId(),user.getUserId());
        return R.ok(subjectVoList);
    }



    @Login
    @PostMapping("moni/end")
    @ApiOperation("结束练习，提交练习")
    public R MoniEnd(@LoginUser UserEntity user,@RequestBody MoniRecordDto moniRecordDto){
        MoniRecordEntity moniRecordEntity = new MoniRecordEntity().setUid((long)user.getUserId())
                .setResult(moniRecordDto.getResult())
                .setSubjectCount(moniRecordDto.getSubjectCount())
                .setCountRight(moniRecordDto.getCountRight())
                .setCountError(moniRecordDto.getCountError())
                .setCourseId(moniRecordDto.getCourseId())
                .setStartTime(moniRecordDto.getStartTime())
                .setSubmitTime(moniRecordDto.getSubmitTime());
        return R.ok(moniRecordService.save(moniRecordEntity)).put("alertMag","临时的内容");
    }


    /**
     * 临时接受JSON 数据的dto,  专为方法 getSubjectBySeIds 提供
     */
    @Data
    public static class SecondtypeIdsRandomDto{
        private List<Long> secondtypeIds;
        private int random=0;
    }

    @Data
    public static class PramsDto{
        private Long id;
    }

    @Data
    public static class MoniRecordDto{
        private Integer result;
        private Integer countRight;
        private Integer countError;
        private Integer subjectCount;
        private Integer courseId;
        private Date startTime;
        private Date submitTime;
    }

}

